package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.impl.dao.WritTypeDao;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.WritType;
import com.jiantong.lawEnforcing.service.WritTypeService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 案件类型
 * Created by mz03 on 2017/9/7.
 */
@Slf4j
@Service
@RpcProvider
public class WritTypeServiceImpl implements WritTypeService {

    private final WritTypeDao writTypeDao;

    public WritTypeServiceImpl(WritTypeDao writTypeDao) {
        this.writTypeDao = writTypeDao;
    }


    /**
     * id 查找
     *
     * @param id
     * @return News
     */
    @Override
    public Response<WritType> finById(Long id) {
        try {
            WritType writType = writTypeDao.findById(id);
            if (writType == null) {
                log.error("writType (id={}) is not found", id);
                return Response.fail("casesBasic.not.found");
            }
            return Response.ok(writType);
        }catch (Exception e){
            log.error("failed to find writType(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("writType.find.fail");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param writType 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(WritType writType) {
       try{
           writTypeDao.create(writType);
           return Response.ok(writType.getId());
       }catch (Exception e){
           log.error("failed.to.create.writType:{}", Throwables.getStackTraceAsString(e));
           return Response.fail("create.writType.failed");
       }
    }

    /**
     * 更新新闻的单据
     *
     * @param writType 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(WritType writType) {
        if (writType.getId()==null){
        }
        try{
            return Response.ok(writTypeDao.update(writType));
        }catch (Exception e){
            log.error("failed to create writType error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("writType.create.failed");
        }
    }

    /**
     * 根据 类型 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param type
     * @param detailed_type
     * @return
     */
    @Override
    public Response<Paging<WritType>> pagingByType(Integer pageNo, Integer pageSize, Integer type, Integer detailed_type) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(type != null){
                criteria.put("type",type);
            }
            if(detailed_type != null){
                criteria.put("detailed_type",detailed_type);
            }

            return Response.ok(writTypeDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find writType(type={}) or (detailed_type = {}) cause:{}", type,detailed_type, Throwables.getStackTraceAsString(e));
            return Response.fail("writType.find.fail");
        }
    }

    /**
     * 查询所有文书一级类型
     *
     * @return Map<Integer, String>
     */
    @Override
    public Response<Map<Integer, String>> allType() {
        try {
            List<WritType> list = writTypeDao.allType();
            Map<Integer,String> map = Maps.newHashMap();
            for (WritType writType : list) {
                map.put(writType.getType(),writType.getType_name());
            }
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find TypeMap cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("TypeMap.find.fail");
        }
    }

    /**
     * 查询二级类型
     *
     * @param type
     * @return
     */
    @Override
    public Response<Map<Integer, String>> allDetailedTypeByType(Integer type) {
        try {
            List<WritType> list = writTypeDao.allDetailedTypeByType(type);
            Map<Integer,String> map = Maps.newHashMap();
            for (WritType writType : list) {
                map.put(writType.getDetailed_type(),writType.getDetailed_type_name());
            }
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find TypeMap cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("TypeMap.find.fail");
        }
    }
}
